
package com.booking.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import com.booking.BuildConfig;
import com.booking.fragment.SplashFragment;
import com.booking.service.GPSService;

/**
 * @author iholub May 22, 2013
 */
public class Splash extends BaseActivity {
    private Handler handler;
    private static final int SPLASH_DELAY = 3000;

    private void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

        alertDialog.setTitle("GPS is settings");
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
                finish();
            }
        });

        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                finish();
            }
        });
        alertDialog.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent locateonService = new Intent(this, GPSService.class);
        startService(locateonService);
        FragmentManager fragmentManager = getSupportFragmentManager();
        if (fragmentManager.findFragmentById(android.R.id.content) == null) {
            Fragment fragment = new SplashFragment();
            fragmentManager.beginTransaction().add(android.R.id.content, fragment).commit();
        }

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            handler = new Handler();
            if (!BuildConfig.DEBUG) {
                handler.postDelayed(runNextIntent, SPLASH_DELAY);
            } else {
                handler.postDelayed(runNextIntent, 700);
            }
        } else {
            showSettingsAlert();
        }
    }

    private Runnable runNextIntent = new Runnable() {
        public void run() {
            Intent intent = new Intent(Splash.this, HomeActivity.class);
            startActivity(intent);
            finish();
        }
    };

    public void onBackPressed() {
        if (handler != null) {
            handler.removeCallbacks(runNextIntent);
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        if (handler != null) {
            handler.removeCallbacks(runNextIntent);
        }
        super.onDestroy();
    }
}
